<template>
    <el-scrollbar
        wrapClass="scrollbar-wrap"
        :style="{height: scrollHeight}"
        ref="scrollbarContainer">
  <div>
    <div class="search-term">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">                      
        <el-form-item>
          <el-button @click="onSubmit" type="primary">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="openDialog" type="primary">新增p201Delivers表</el-button>
        </el-form-item>
        <el-form-item>
          <el-popover placement="top" v-model="deleteVisible" width="160">
            <p>确定要删除吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button @click="deleteVisible = false" size="mini" type="text">取消</el-button>
                <el-button @click="onDelete" size="mini" type="primary">确定</el-button>
              </div>
            <el-button icon="el-icon-delete" size="mini" slot="reference" type="danger">批量删除</el-button>
          </el-popover>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="tableData"
      @selection-change="handleSelectionChange"
      border
      ref="multipleTable"
      stripe
      style="width: 100%"
      tooltip-effect="dark"
    >
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column label="日期" width="180">
         <template slot-scope="scope">{{scope.row.CreatedAt|formatDate}}</template>
    </el-table-column>
    
    <el-table-column label="客户创建时间" prop="custCreateTime" width="120">
         <template slot-scope="scope">{{scope.row.custCreateTime|formatDate}}</template>
    </el-table-column>
    
    <el-table-column label="客户编号" prop="custId" width="120"></el-table-column> 
    
    <el-table-column label="客户名称" prop="custName" width="120"></el-table-column> 
    
    <el-table-column label="客户密钥导入" prop="custkeyImport" width="120"></el-table-column> 
    
    <el-table-column label="发货时间" prop="deliverTime" width="120">
         <template slot-scope="scope">{{scope.row.deliverTime|formatDate}}</template>
    </el-table-column>
    
    <el-table-column label="发货原因" prop="deliverReason" width="120"></el-table-column> 
    
    <el-table-column label="部署模式" prop="deployMode" width="120"></el-table-column> 
    
    <el-table-column label="固件版本" prop="firmwareVer" width="120"></el-table-column> 
    
    <el-table-column label="密钥控制模式" prop="keyCtrlMode" width="120"></el-table-column> 
    
    <el-table-column label="密钥系统版本" prop="kmsVer" width="120"></el-table-column> 
    
    <el-table-column label="平台版本" prop="platVer" width="120"></el-table-column> 
    
      <el-table-column label="按钮组">
        <template slot-scope="scope">
          <el-button @click="updateP201Delivers(scope.row)" size="small" type="primary">变更</el-button>
          <el-popover placement="top" width="160" v-model="scope.row.visible">
            <p>确定要删除吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="scope.row.visible = false">取消</el-button>
              <el-button type="primary" size="mini" @click="deleteP201Delivers(scope.row)">确定</el-button>
            </div>
            <el-button type="danger" icon="el-icon-delete" size="mini" slot="reference">删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="page"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <el-dialog :before-close="closeDialog" :visible.sync="dialogFormVisible" title="【P201发货】录入发货记录">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="120px">
        <el-form-item label="客户编号" prop="custId">
          <el-input v-model="formData.custId" placeholder="请输入客户编号" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="客户名称" prop="custName">
          <el-input v-model="formData.custName" placeholder="请输入客户名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="客户创建时间" prop="custCreateTime">
          <el-date-picker v-model="formData.custCreateTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
            :style="{width: '100%'}" placeholder="请选择客户创建时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="系统固件版本" prop="firmwareVer">
          <el-input v-model="formData.firmwareVer" placeholder="请输入系统固件版本" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="P201部署模式" prop="deployMode">
          <el-select v-model="formData.deployMode" placeholder="请选择P201部署模式" clearable
            :style="{width: '100%'}">
            <el-option v-for="(item, index) in deployModeOptions" :key="index" :label="item.label"
              :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="密钥系统版本" prop="kmsVer">
          <el-input v-model="formData.kmsVer" placeholder="请输入密钥系统版本" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="密钥控制模式" prop="keyCtrlMode">
          <el-radio-group v-model="formData.keyCtrlMode" size="medium">
            <el-radio v-for="(item, index) in keyCtrlModeOptions" :key="index" :label="item.value"
              :disabled="item.disabled">{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="密钥是否导入" prop="custkeyImport">
          <el-radio-group v-model="formData.custkeyImport" size="medium">
            <el-radio v-for="(item, index) in custkeyImportOptions" :key="index" :label="item.value"
              :disabled="item.disabled">{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="发货时间" prop="deliverTime">
          <el-date-picker v-model="formData.deliverTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
            :style="{width: '100%'}" placeholder="请选择发货时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="平台版本" prop="platVer">
          <el-select v-model="formData.platVer" placeholder="请选择平台版本" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in platVerOptions" :key="index" :label="item.label"
              :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发货原因" prop="deliverReason">
          <el-select v-model="formData.deliverReason" placeholder="请选择发货原因" clearable
            :style="{width: '100%'}">
            <el-option v-for="(item, index) in deliverReasonOptions" :key="index" :label="item.label"
              :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="enterDialog" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
    </el-scrollbar>
</template>

<script>
import {
    createP201Delivers,
    deleteP201Delivers,
    deleteP201DeliversByIds,
    updateP201Delivers,
    findP201Delivers,
    getP201DeliversList
} from "@/api/p201Delivers";  //  此处请自行替换地址
import { formatTimeToStr } from "@/utils/data";
import infoList from "@/components/mixins/infoList";

export default {
  name: "P201Delivers",
  mixins: [infoList],
  data() {
    return {
            scrollHeight:'0px',
      listApi: getP201DeliversList,
      dialogFormVisible: false,
      visible: false,
      type: "",
      deleteVisible: false,
      multipleSelection: [],
      formData: {
        custId: undefined,
        custName: undefined,
        custCreateTime: null,
        firmwareVer: undefined,
        deployMode: undefined,
        kmsVer: undefined,
        keyCtrlMode: 1,
        custkeyImport: 1,
        deliverTime: null,
        platVer: undefined,
        deliverReason: undefined,
      },
      rules: {
        custId: [{
          required: true,
          message: '请输入客户编号',
          trigger: 'blur'
        }],
        custName: [{
          required: true,
          message: '请输入客户名称',
          trigger: 'blur'
        }],
        custCreateTime: [],
        firmwareVer: [{
          required: true,
          message: '请输入系统固件版本',
          trigger: 'blur'
        }],
        deployMode: [{
          required: true,
          message: '请选择P201部署模式',
          trigger: 'change'
        }],
        kmsVer: [{
          required: true,
          message: '请输入密钥系统版本',
          trigger: 'blur'
        }],
        keyCtrlMode: [{
          required: true,
          message: '密钥控制模式不能为空',
          trigger: 'change'
        }],
        custkeyImport: [{
          required: true,
          message: '密钥是否导入不能为空',
          trigger: 'change'
        }],
        deliverTime: [{
          required: true,
          message: '请选择发货时间',
          trigger: 'change'
        }],
        platVer: [{
          required: true,
          message: '请选择平台版本',
          trigger: 'change'
        }],
        deliverReason: [{
          required: true,
          message: '请选择发货原因',
          trigger: 'change'
        }],
      },
      deployModeOptions: [{
        "label": "Linux部署",
        "value": 1
      },{
        "label": "Docker部署",
        "value": 2
      }],
      keyCtrlModeOptions: [{
        "label": "公司控制",
        "value": 1
      }, {
        "label": "现场控制",
        "value": 2
      }],
      custkeyImportOptions: [{
        "label": "已导入",
        "value": 1
      }, {
        "label": "未导入",
        "value": 2
      }],
      platVerOptions: [{
        "label": "V8",
        "value": 1
      }, {
        "label": "N8",
        "value": 2
      }, {
        "label": "N6",
        "value": 3
      }, {
        "label": "T6",
        "value": 4
      }],
      deliverReasonOptions: [{
        "label": "新客户",
        "value": 1
      }, {
        "label": "老客户升级",
        "value": 2
      }, {
        "label": "换货",
        "value": ""
      }],
    };
  },
  filters: {
    formatDate: function(time) {
    //    console.log(time)
    //    console.log("--|-- 时间格式化)
      if (time != null && time != "") {
        var date = new Date(time);
        return formatTimeToStr(date, "yyyy-MM-dd hh:mm:ss");
      } else {
        return "";
      }
    },
    formatBoolean: function(bool) {
      if (bool != null) {
        return bool ? "是" :"否";
      } else {
        return "";
      }
    }
  },
  methods: {
      //条件搜索前端看此方法
      onSubmit() {
        this.page = 1
        this.pageSize = 10               
        this.getTableData()
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      async onDelete() {
        const ids = []
        this.multipleSelection &&
          this.multipleSelection.map(item => {
            ids.push(item.ID)
          })
        const res = await deleteP201DeliversByIds({ ids })
        if (res.code == 0) {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.deleteVisible = false
          this.getTableData()
        }
      },
    async updateP201Delivers(row) {
      const res = await findP201Delivers({ ID: row.ID });
      this.type = "update";
      if (res.code == 0) {
        this.formData = res.data.rep201Delivers;
        this.dialogFormVisible = true;
      }
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.formData = {
        
          custCreateTime:null,
          custId:null,
          custName:null,
          custkeyImport:null,
          deliverTime:null,
          deliverReason:null,
          deployMode:null,
          firmwareVer:null,
          keyCtrlMode:null,
          kmsVer:null,
          platVer:null,
      };
    },
    async deleteP201Delivers(row) {
      this.visible = false;
      const res = await deleteP201Delivers({ ID: row.ID });
      if (res.code == 0) {
        this.$message({
          type: "success",
          message: "删除成功"
        });
        this.getTableData();
      }
    },
    async enterDialog() {
      console.log("-----------------")
      let res;
      switch (this.type) {
        case "create":
        //    console.log("------------- formdata -------------")
        //    console.log(this.formData);
            var strCustCreateTime;
          strCustCreateTime = this.formData.custCreateTime;
          if (strCustCreateTime.length < 20) {
              this.formData.custCreateTime += "T01:01:01Z";
          }
          if (this.formData.deliverTime.length < 20) {
              this.formData.deliverTime += "T01:01:01Z"
          }
            console.log("------------- replace -------------")
            console.log(this.formData)
          res = await createP201Delivers(this.formData);
          break;
        case "update":
        //2020-09-14T10:25:06Z
        console.log(typeof this.formData.custCreateTime);
        if(this.formData.custCreateTime.length < 20) {
                    this.formData.custCreateTime += "T01:01:01Z"
        }
        if(this.formData.deliverTime.length < 20) {
                    this.formData.deliverTime += "T01:01:01Z"
        }
          res = await updateP201Delivers(this.formData);
          break;
        default:
          res = await createP201Delivers(this.formData);
          break;
      }
      if (res.code == 0) {
        this.$message({
          type:"success",
          message:"创建/更改成功"
        })
        this.closeDialog();
        this.getTableData();
      }
    },
    openDialog() {
      this.type = "create";
      this.dialogFormVisible = true;
    }
  },
  async created() {
    await this.getTableData();
  },
    mounted(){
        this.scrollHeight = window.innerHeight*0.7 + 'px';
    }
};
</script>

<style>
</style>
